package com.etrans.web.gps.util.map;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.etrans.web.gps.util.HttpUtil;


/**
 * @author grc Date 2017/07/03 高德地图工具类 *
 * 
 */
public class GaoDeUtils {

	/**
	 * 通过高德地图查询地址
	 * 
	 * @param 经纬度以数组方式存储,最多支持20个坐标点
	 * @param batch=true为批量查询。batch=false为单点查询
	 */
	
	public static List<String> getPosition(String lon[], String lat[],String keys) {
		boolean batch= true;
		String[] key = keys.split(",");
		String url = "";
		String baseurl = "http://restapi.amap.com/v3/geocode/regeo?key=";
		String location = "&location=";
		String batchs = "&batch=" + batch;
		String extensions="&extensions=base";
		List<String> list = new ArrayList<>();
		for (int i = 0; i < lon.length; i++) {
			if (i == lon.length - 1) {
				location += lon[i] + "," + lat[i];
			} else {
				location += lon[i] + "," + lat[i] + "|";
			}
		}
		// 取0-key.lenth范围随机数
		Random random = new Random();
		int n = random.nextInt(key.length);
		url = baseurl + key[n] + location +extensions+ batchs;
		String response = HttpUtil.sendGet(url);
		Map<String, Object> result = JSON.parseObject(response, Map.class);
		if (!"10000".equals(result.get("infocode"))) {
			return null;
		}
			
		List<Map> allList = (List) result.get("regeocodes");
		
		for (int i = 0; i < allList.size(); i++) {
			Map map = allList.get(i);
			String str = map.get("formatted_address").toString();
			if(str.equals("[]")){
				list.add("");
			}else{
				list.add(str);
			}
			
		}
		return list;
	}

	public static void main(String args[]){
		String[] lon={"110.11488" ,"114.76877"}; 
		String[] lat={"20.178934","23.519234"};
		boolean batch=false;
		//getPosition(lon, lat);
	}
	
}
